package com.kizuki.thread;

import org.junit.Test;

// 停止不推荐stop 推荐自己停下来或者boolean
public class TestThreadApi implements Runnable{

    @Override
    public void run() {
    }

    @Test
    public void testSleep () throws InterruptedException {
        new Thread(new TestThread2()).start();
        new Thread(new TestThread2()).start();
        Thread.sleep(1000); // 休眠
        // lambda适用 只有一个抽象方法的函数式接口，如Runnable
        new Thread(() -> System.out.println("lambda")).start();
    }


    @Test
    public void testJoin () throws InterruptedException {
        Thread thread = new Thread(new TestThread3());
        for (int i = 0;i < 40; i ++) {
            if (i == 20) {
                thread.start();
                thread.join();
            }
            System.out.println("main_" + i);
        }
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new TestThread3());
        Thread thread1 = new Thread(new TestThread3());
        thread.setPriority(5); // 默认是5
        thread1.setPriority(10); //
        thread.start();
        thread1.setDaemon(true); // 设置为守护线程  程序不等待执行完毕
        thread1.start();
    }
}

class TestThread2 implements Runnable {

    @Override
    public void run() { // 礼让 cpu重新调度
        System.out.println(Thread.currentThread().getName() + "开始");
        Thread.yield();
        System.out.println(Thread.currentThread().getName() + "结束");
    }
}

class TestThread3 implements Runnable {

    @Override
    public void run() { // join 阻塞
        for (int i = 0;i < 50; i ++) {
            System.out.println(Thread.currentThread().getName() + "开始");
        }
    }
}
